import axios from 'axios'

const instance = axios.create({
        baseURL: 'http://1.15.89.168/',
        timeout: 9000,
})

export function post(url, params) {
        return new Promise((resolve, reject) => {
                instance.post(url, params).then(res => {
                        resolve(res)
                }).catch(err => {
                        reject(err)
                })
        })
}

export function get(url) {
        return new Promise((resolve, reject) => {
                instance.get(url).then(res => {
                        resolve(res);
                }).catch(err => {
                        reject(err)
                })
        })
}

export function getComments(url,params) {
    return new Promise((resolve, reject) => {
            instance.get(url,{
                params:{
                    write_id:params
                }
            }).then(res => {
                    resolve(res);
            }).catch(err => {
                    reject(err)
            })
    })
}

instance.interceptors.request.use(config => {
        if (localStorage.getItem("token")) { //如果token存在，则请求头上面携带token给后端传输
                let token = JSON.parse(localStorage.getItem("token"))
                config.headers.Authorization = 'Bearer ' + token
        }
        return config;
},
        err => {
                return Promise.reject(err);
        });

instance.interceptors.response.use(res => {
        if (res.status == 200) {
                //
        }
        return res;
},
        err => {
                return Promise.reject(err)
        }
)

export default instance;

